<?php
include_once 'MySQLDAO.php';

/**
 * Definition of Class Address
 * This class represents a data access object for the database table 'address'.
 */
class Address {


	private $dao = null;

	private $i = 1;

	/**
	 * associated database field: addressId, type: int, is nullable: false
	 */
	private $addressId  = 0;

	/**
	 * associated database field: street, type: varchar, is nullable: true, character set: utf8, max length: 100
	 */
	private $street;

	/**
	 * associated database field: city, type: varchar, is nullable: true, character set: utf8, max length: 45
	 */
	private $city;

	/**
	 * associated database field: stateOrProvince, type: varchar, is nullable: true, character set: utf8, max length: 45
	 */
	private $stateOrProvince;

	/**
	 * associated database field: postalCode, type: varchar, is nullable: true, character set: utf8, max length: 20
	 */
	private $postalCode;


	/**
	 * Default constructor
	 * @param value some value
	 */
	function __construct(){
		$this->dao = new MySQLDao("Address");
	}


	/**
	 * Initialize the business object with data read from the DB.
	 * @param row database row array containing the values from one read record
	 */
	public function init($row){
		if(isset($row['addressId'])) $this->addressId = $row['addressId'];
		if(isset($row['street'])) $this->street = $row['street'];
		if(isset($row['city'])) $this->city = $row['city'];
		if(isset($row['stateOrProvince'])) $this->stateOrProvince = $row['stateOrProvince'];
		if(isset($row['postalCode'])) $this->postalCode = $row['postalCode'];
	}


	/**
	 * Returns the string representation for this object
	 * @param escapeQuotationMarks boolean value to indicate if quotation marks in value strings should be escaped, default value is true
	 * @return String String representation of Address
	 */
	public function toString($escapeQuotationMarks=true){
		$s = '[';
		$s .= 'addressId: '.(($escapeQuotationMarks===true) ? addslashes($this->addressId) : $this->addressId).', ';
		$s .= 'street: '.(($escapeQuotationMarks===true) ? addslashes($this->street) : $this->street).', ';
		$s .= 'city: '.(($escapeQuotationMarks===true) ? addslashes($this->city) : $this->city).', ';
		$s .= 'stateOrProvince: '.(($escapeQuotationMarks===true) ? addslashes($this->stateOrProvince) : $this->stateOrProvince).', ';
		$s .= 'postalCode: '.(($escapeQuotationMarks===true) ? addslashes($this->postalCode) : $this->postalCode);
		$s .= ']';
		return $s;
	}


	/**
	 * Returns the JSON (JavaScript Object Notation) string representation for this object
	 * @return String JSON representation of Address
	 */
	public function toJSONString(){
		$fieldValues = array ("addressId"=>$this->addressId, "street"=>$this->street, "city"=>$this->city, "stateOrProvince"=>$this->stateOrProvince, "postalCode"=>$this->postalCode);
		return json_encode($fieldValues);
	}


	/**
	 * Returns a HTML representation of this object using DIVs for formatting via css
	 * @param escapeHTML boolean value to indicate if value strings should be html escaped, default value is true
	 * @return String the HTML representation of the Address object
	 */
	public function toHTMLString($escapeHTML=true){
		$htmlCode = "<div class=\"address\">\n";
		$htmlCode .= '	<div class="address_addressId">'.(($escapeHTML===true) ? htmlspecialchars($this->addressId) : $this->addressId)."</div>\n";
		$htmlCode .= '	<div class="address_street">'.(($escapeHTML===true) ? htmlspecialchars($this->street) : $this->street)."</div>\n";
		$htmlCode .= '	<div class="address_city">'.(($escapeHTML===true) ? htmlspecialchars($this->city) : $this->city)."</div>\n";
		$htmlCode .= '	<div class="address_stateOrProvince">'.(($escapeHTML===true) ? htmlspecialchars($this->stateOrProvince) : $this->stateOrProvince)."</div>\n";
		$htmlCode .= '	<div class="address_postalCode">'.(($escapeHTML===true) ? htmlspecialchars($this->postalCode) : $this->postalCode)."</div>\n";
		$htmlCode .= "</div>\n";
		return $htmlCode;
	}
	
	public function getData($where) {
		$rows = $this->dao->getData($where);
		return $rows;
	}


	public static function loadAll(){
		$address = new Address();
		$rows = $address->dao->getData("");
		$addresss = array();
		for ($index = 0; $index < sizeof($rows); $index++) {
			$d = new Address();
			$d->init($rows[$index]);
			array_push($addresss,$d);
		}
		return $addresss;
	}


	/**
	 * Load the Address uniquely by its primary key.
	 * @param addressId the primary key
	 * @return Address Instance of {@link Address}
	 */
	public static function loadUnique($addressId){
		$address = new Address();
		$rows = $address->dao->getData("`addressId`='$addressId'");
		$address->init($rows[0]);
		return $address;
	}


	/**
	 * Insert this object into the DB
	 * @return number new id (auto increment value) generated
	 */
	public function insert(){
		//$list = array("addressId"=>$this->addressId, "street"=>$this->street, "city"=>$this->city, "stateOrProvince"=>$this->stateOrProvince, "postalCode"=>$this->postalCode);
		$list = array("street"=>$this->street, "city"=>$this->city, "stateOrProvince"=>$this->stateOrProvince, "postalCode"=>$this->postalCode);
		return $this->dao->insertRecord($list);
	}


	/**
	 * Update this object into the DB
	 * @return number the number of updated records
	 */
	public function update(){
		$list = array("addressId"=>$this->addressId, "street"=>$this->street, "city"=>$this->city, "stateOrProvince"=>$this->stateOrProvince, "postalCode"=>$this->postalCode);
		$where = "`addressId`='$addressId'";
		return $this->dao->updateRecord($list,$where);
	}


	/**
	 * Initialize the business object with data read from the DB.
	 */
	public function initPOST(){
		if (isset($_POST['addressId'])){
			$this->addressId = $_POST['addressId'];
		}
		if (isset($_POST['StreetAddress'])){
			$this->street = $_POST['StreetAddress'];
		}
		if (isset($_POST['City'])){
			$this->city = $_POST['City'];
		}
		if (isset($_POST['StateOrProvince'])){
			$this->stateOrProvince = $_POST['StateOrProvince'];
		}
		if (isset($_POST['ZIP'])){
			$this->postalCode = $_POST['ZIP'];
		}
	}


	/**
	 * Initialize the business object with data read from the DB.
	 */
	public function initGET(){
		if (isset($_GET['addressId'])){
			$this->addressId = $_GET['addressId'];
		}
		if (isset($_GET['StreetAddress'])){
			$this->street = $_GET['StreetAddress'];
		}
		if (isset($_GET['City'])){
			$this->city = $_GET['City'];
		}
		if (isset($_GET['StateOrProvince'])){
			$this->stateOrProvince = $_GET['StateOrProvince'];
		}
		if (isset($_GET['ZIP'])){
			$this->postalCode = $_GET['ZIP'];
		}
	}


	/**
	 * Returns the value from the database field addressId.
	 * In the database the field addressId is of type int.
	 * The database field is NOT nullable.
	 * @return String the value of the field
	 */
	public function getAddressId(){
		return $this->addressId;
	}


	/**
	 * Set the value from the database field addressId.
	 * In the database the field addressId is of type int.
	 * The database field is NOT nullable.
	 * @param addressId String with the value for the field to be set
	 */
	public function setAddressId($addressId){
		$this->addressId = $addressId;
	}


	/**
	 * Returns the value from the database field street.
	 * In the database the field street is of type varchar.
	 * The database field is  nullable.
	 * @return String the value of the field
	 */
	public function getStreet(){
		return $this->street;
	}


	/**
	 * Set the value from the database field street.
	 * In the database the field street is of type varchar.
	 * The database field is  nullable.
	 * @param street String with the value for the field to be set
	 */
	public function setStreet($street){
		if(strlen($street) > 100) throw new Exception("street does not allow strings longer than 100 characters. The submitted string contains ".strlen($street)." characters.");
		$this->street = $street;
	}


	/**
	 * Returns the value from the database field city.
	 * In the database the field city is of type varchar.
	 * The database field is  nullable.
	 * @return String the value of the field
	 */
	public function getCity(){
		return $this->city;
	}


	/**
	 * Set the value from the database field city.
	 * In the database the field city is of type varchar.
	 * The database field is  nullable.
	 * @param city String with the value for the field to be set
	 */
	public function setCity($city){
		if(strlen($city) > 45) throw new Exception("city does not allow strings longer than 45 characters. The submitted string contains ".strlen($city)." characters.");
		$this->city = $city;
	}


	/**
	 * Returns the value from the database field stateOrProvince.
	 * In the database the field stateOrProvince is of type varchar.
	 * The database field is  nullable.
	 * @return String the value of the field
	 */
	public function getStateOrProvince(){
		return $this->stateOrProvince;
	}


	/**
	 * Set the value from the database field stateOrProvince.
	 * In the database the field stateOrProvince is of type varchar.
	 * The database field is  nullable.
	 * @param stateOrProvince String with the value for the field to be set
	 */
	public function setStateOrProvince($stateOrProvince){
		if(strlen($stateOrProvince) > 45) throw new Exception("stateOrProvince does not allow strings longer than 45 characters. The submitted string contains ".strlen($stateOrProvince)." characters.");
		$this->stateOrProvince = $stateOrProvince;
	}


	/**
	 * Returns the value from the database field postalCode.
	 * In the database the field postalCode is of type varchar.
	 * The database field is  nullable.
	 * @return String the value of the field
	 */
	public function getPostalCode(){
		return $this->postalCode;
	}


	/**
	 * Set the value from the database field postalCode.
	 * In the database the field postalCode is of type varchar.
	 * The database field is  nullable.
	 * @param postalCode String with the value for the field to be set
	 */
	public function setPostalCode($postalCode){
		if(strlen($postalCode) > 20) throw new Exception("postalCode does not allow strings longer than 20 characters. The submitted string contains ".strlen($postalCode)." characters.");
		$this->postalCode = $postalCode;
	}
}
?>